<?php


namespace App\Service\Engine;


use App\Web\Model\Model;
use EchoPHP\Facades\Log;
use EchoPHP\Support\Str;

class ResumeEngine extends EngineBase {

    /**
     * 简历引擎最大返回数目
     * @var int
     */
    protected $maxRowNum = 5000;
    /**
     * 每次请求接口返回数目
     * @var int
     */
    protected $perCallSearchCount = 200;

    public function __construct() {
        parent::__construct('resume');
    }

    /**
     * mixEngine 搜索字段
     * @var array
     */
    private static $searchField = [
        'serverno' => ['default' => '0', 'maxcount' => 1, 'separator' => '#'],    // 服务器编号，固定0
        'searchtype' => ['default' => '0', 'maxcount' => 1, 'separator' => '#'],    // 搜索类型，0~5
        'sorttype' => ['default' => '0', 'maxcount' => 1, 'separator' => '#'],    // 排序方式，0-相关度 1-时间
        'isexpectarea' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否包含期望工作地
        'area' => ['default' => '000000', 'maxcount' => 5, 'separator' => '|'],    // 居住地: 省/市/区
        'workfunc' => ['default' => '0000', 'maxcount' => 3, 'separator' => '|'],    // 工作职能
        'workindustry' => ['default' => '00', 'maxcount' => 3, 'separator' => '|'],    // 工作行业
        'companytype' => ['default' => '00', 'maxcount' => 5, 'separator' => '|'],    // 公司性质
        'topmajor' => ['default' => '0000', 'maxcount' => 3, 'separator' => '|'],    // 主修专业
        'updatedate_min' => ['default' => '00000000', 'maxcount' => 1, 'separator' => '|'],    // 从更新日期
        'updatedate_max' => ['default' => '00000000', 'maxcount' => 1, 'separator' => '|'],    // 到更新日期
        'lastapplydate_min' => ['default' => '00000000', 'maxcount' => 1, 'separator' => '|'],    // 从投递时间
        'lastapplydate_max' => ['default' => '00000000', 'maxcount' => 1, 'separator' => '|'],    // 到投递时间
        'openstatus' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 简历公开程度
        'currentsituation' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 求职状态
        'currentsalary_min' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最低目前薪资
        'currentsalary_max' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最高目前薪资
        'expectsalary_min' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最低期望薪资
        'expectsalary_max' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最高期望薪资
        'workyear_min' => ['default' => '0000', 'maxcount' => 1, 'separator' => '|'],    // 最小工作起始年份
        'workyear_max' => ['default' => '0000', 'maxcount' => 1, 'separator' => '|'],    // 最大工作起始年份
        'degree_min' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最低学历
        'degree_max' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最高学历
        'age_min' => ['default' => '', 'maxcount' => 1, 'separator' => '|'],    // 最低年龄
        'age_max' => ['default' => '', 'maxcount' => 1, 'separator' => '|'],    // 最高年龄
        'stature_min' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最小身高
        'stature_max' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 最大身高
        'resumesource' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 简历来源
        'gender' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 性别
        'hasereusme' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否包含英文简历
        'ismba' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否mba
        'haspic' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否有简历照片
        'seektype' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 工作类型
        'hukou' => ['default' => '000000', 'maxcount' => 1, 'separator' => '|'],    // 户口
        'enlevel' => ['default' => '0000', 'maxcount' => 1, 'separator' => '|'],    // 英语等级
        'jplevel' => ['default' => '0000', 'maxcount' => 1, 'separator' => '|'],    // 日语等级
        'expectarea' => ['default' => '000000', 'maxcount' => 5, 'separator' => '|'],    // 期望工作地
        'onlycurworkfunc' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否只搜当前工作职能
        'onlycurindustry' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否只搜当前工作行业
        'graduatetype' => ['default' => '0', 'maxcount' => 1, 'separator' => '|'],    // 是否在校生
        'graduateyear' => ['default' => '0000', 'maxcount' => 1, 'separator' => '|'],    // 毕业年份
        'onlycurcomp' => ['default' => '9', 'maxcount' => 1, 'separator' => '|'],    // 是否只搜当前公司
        'nstartid' => ['default' => '1', 'maxcount' => 1, 'separator' => '#'],    // 开始记录号
        'nendid' => ['default' => '200', 'maxcount' => 1, 'separator' => '#'],    // 结束记录号
        'ehirename' => ['default' => '', 'maxcount' => 1, 'separator' => 'chr16'],    // 网才会员hr公司名称
        'comptext' => ['default' => '', 'maxcount' => 1, 'separator' => 'chr16', 'charset' => 'gbk'],    // 公司筛选
        'searchstring' => ['default' => '', 'maxcount' => 1, 'separator' => 'chr16', 'charset' => 'gbk'],    // 搜索字串
        'extendstring' => ['default' => '', 'maxcount' => 1, 'separator' => 'chr16', 'charset' => 'gbk'],    // 扩张字段
        'graduateschool' => ['default' => '', 'maxcount' => 1, 'separator' => 'chr16', 'charset' => 'gbk'],    // 毕业院校
        'jobtext' => ['default' => '', 'maxcount' => 1, 'separator' => '', 'charset' => 'gbk'],    // 职位筛选
    ];

    /**
     * 薪资范围字典
     * @var array
     */
    private static $salaryRange = [
        '01' => ['min' => '0', 'max' => '03'], // 3k及以下
        '02' => ['min' => '04', 'max' => '04'], // 3k-4.5k
        '03' => ['min' => '05', 'max' => '05'], // 4.5k-6k
        '04' => ['min' => '06', 'max' => '06'], // 6k-8k
        '05' => ['min' => '07', 'max' => '07'], // 8k-10k
        '06' => ['min' => '08', 'max' => '08'], // 10k-15k
        '07' => ['min' => '09', 'max' => '09']  // 15k-20k
    ];

    /**
     * 工作年限字典
     * @var array
     */
    private static $workyearRange = [
        '01' => ['min' => 0, 'max' => 1], // 1年
        '02' => ['min' => 2, 'max' => 2], // 2年
        '03' => ['min' => 3, 'max' => 4], // 3-4年
        '04' => ['min' => 5, 'max' => 7], // 5-7年
        '05' => ['min' => 8, 'max' => 9], // 8-9年
        '06' => ['min' => 10, 'max' => 9999], // 10年以上
    ];

    /**
     * 简历更新时间字典
     * @var array
     */
    private static $resumeUpdatedateRange = [
        '1' => ['min' => '-1 week', 'max' => ''], // 近1周
        '2' => ['min' => '-2 week', 'max' => ''], // 近2周
        '3' => ['min' => '-1 month', 'max' => ''], // 近1个月
        '4' => ['min' => '-2 month', 'max' => ''], // 近2个月
        '5' => ['min' => '-6 month', 'max' => ''], // 近6个月
        '6' => ['min' => '-1 year', 'max' => ''], // 近1年
        '7' => ['min' => '', 'max' => '-1 year']  // 1年及以上
    ];

    /**
     * 学历字典
     * @var array
     */
    private static $degreeRange = [
        '01' => ['min' => 6, 'max' => 8], // 本科及以上
        '02' => ['min' => 7, 'max' => 8], // 硕士及以上
        '03' => ['min' => 6, 'max' => 6], // 本科
        '04' => ['min' => 7, 'max' => 7], // 硕士
        '05' => ['min' => 8, 'max' => 8], // 博士
    ];

    /**
     * 获取引擎数据查询限制
     * @return array
     */
    public function getEngineLimit() {
        return [$this->maxRowNum, $this->perCallSearchCount];
    }

    /**
     * 简历引擎调用
     * @param $params
     * @param $pageNum
     * @param $perPageCount
     * @return array
     *      callresult 引擎调用结果  -1 调用出现错误 0 未调用引擎 1 调用成功 2 传入纯数字，直接查询数据库
     *      allMgrCount 总结果数目
     *      searchTime 引擎执行耗时
     *      mgrCount    当前调用结果总数
     *      mgrIDArr 当前调用返回简历id明细
     *      sendbuf  调用引擎发送串
     */
    public function search($params, $pageNum, $perPageCount) {
        //每页显示数目特殊处理，防止出现0条情况
        $perPageCount = $perPageCount > 0 ? $perPageCount : 10;
        //整理数据，判断是否需要进行引擎调用
        //获取最后调用引擎时的查询页码
        $lastCallEnginePageNum = isset($params['lastCallEnginePageNum']) ? intval($params['lastCallEnginePageNum']) : 1;
        $lastCallEnginePageNum = $lastCallEnginePageNum > 1 ? $lastCallEnginePageNum : 1;
        $cachedResumeId = isset($params['cachedResumeId']) ? (string)$params['cachedResumeId'] : '';
        $cachedResumeIdArr = explode(',', $cachedResumeId);
        //查询页码处理，避免查询导出引擎给出范围的数据
        $pageNum = intval($pageNum) > 0 ? intval($pageNum) : 1;
        $pageNum = ($pageNum > $this->maxRowNum / $perPageCount) ? $this->maxRowNum / $perPageCount : $pageNum;
        if ($lastCallEnginePageNum < $pageNum && ($pageNum - $lastCallEnginePageNum) * $perPageCount < count($cachedResumeIdArr)) {
            //已缓存简历id足够当次简历搜索需求,则直接从缓存中获取
            return [
                'callresult' => 0,
                'allMgrCount' => $params['totalcount'],
                'searchTime' => 0,
                'mgrCount' => count($cachedResumeIdArr),
                'mgrIDArr' => $cachedResumeIdArr,
                'sendbuf' => '',
                'pageNum' => $pageNum,
                'lastCallEnginePageNum' => $lastCallEnginePageNum,
            ];
        }
        $lastCallEnginePageNum = $pageNum;

        if (!empty($params['fulltext'])) {
            $model = new Model();
            $fullTextArr = $model->modifySearchArr($params['fulltext']);
            $isAllNumber = array_reduce($fullTextArr, function ($carry, $item) {
                return $carry && is_numeric($item);
            }, true);
            if ($isAllNumber) {
                //传入全部是数字，则直接调用数据库查询  最多查询一页数据
                $fullTextArr = array_slice($fullTextArr, 0, $perPageCount);
                return [
                    'callresult' => 2,
                    'allMgrCount' => count($fullTextArr),
                    'searchTime' => 0,
                    'mgrCount' => count($fullTextArr),
                    'mgrIDArr' => $fullTextArr,
                    'sendbuf' => '',
                    'pageNum' => $pageNum,
                    'lastCallEnginePageNum' => $lastCallEnginePageNum,
                ];
            }
        }
        //职能字典升级过渡使用
        $params = $this->functypeTransKeywords($params);

        $sendBuf = $this->prepareSendBuf($params, $pageNum, $perPageCount);
        $convertedSendBuf = Str::convertCharset($sendBuf, 'utf8', 'gbk');
        if (!$this->callEngine($sendBuf)) {
            $errMsg = '简历引擎出错[code:' . $this->getErrorCode() . ';message:' . $this->getErrorMsg() . ']';
            $params['sendbuf'] = $convertedSendBuf;
            $params['receivebuf'] = $this->receiveBuf;
            Log::error($errMsg, $params);
            return [
                'callresult' => -1,
                'allMgrCount' => 0,
                'searchTime' => 0,
                'mgrCount' => 0,
                'mgrIDArr' => [],
                'sendbuf' => $convertedSendBuf,
                'pageNum' => $pageNum,
                'lastCallEnginePageNum' => $lastCallEnginePageNum,
            ];
        }
        $result = $this->parseReceiveBuf();
        $result['pageNum'] = $pageNum;
        $result['lastCallEnginePageNum'] = $lastCallEnginePageNum;
        $result['sendbuf'] = $convertedSendBuf;
        return $result;
    }

    /**
     * 整理sendbuf
     * @param array $params
     * @param int $pageNum
     * @param int $perPageCount
     * @return mixed|string
     */
    protected function prepareSendBuf(array $params, $pageNum = 1, $perPageCount = 10) {
        $treatedParams = $this->prepareSearchParams($params);
        // 根据当前页码控制查询范围
        $startRowNum = ($pageNum - 1) * $perPageCount + 1;
        $startRowNum = $startRowNum >= $this->maxRowNum ? ($this->maxRowNum - $perPageCount) : $startRowNum;
        $endRowNum = ($startRowNum + $this->perCallSearchCount - 1) > $this->maxRowNum ? $this->maxRowNum : ($startRowNum + $this->perCallSearchCount - 1);
        $treatedParams['nstartid'] = $startRowNum;
        $treatedParams['nendid'] = $endRowNum;
        $treatedParams['serverno'] = '0';

        return $this->mergeSendBuf($treatedParams);
    }

    /**
     * 找精英表单数据预处理，按照mixEngine需求进行标准化转换
     * @param $params
     * @return array
     */
    protected function prepareSearchParams($params) {
        $treated = ['extendstring' => []];
        if (strlen($params['fulltext']) > 0) {     // 关键字搜索
            $treated['searchstring'] = $params['fulltext'];
        }
        if (strlen($params['jobtext']) > 0) {     // 职位名称
            $treated['jobtext'] = $params['jobtext'];
        }
        if (strlen($params['graduateschool']) > 0) {     // 毕业院校
            $treated['graduateschool'] = $params['graduateschool'];
        }
        if (strlen($params['cotext']) > 0) {   // 公司
            $treated['comptext'] = $params['cotext'];
        }
        if ($params['onlycurcomp'] == '1') {    // 只搜最近公司
            $treated['onlycurcomp'] = 1;
        }
        if (!empty($params['graduateyear'])) {    // 毕业年份
            $treated['graduateyear'] = $params['graduateyear'];
            $treated['graduatetype'] = 1;
        }
        if (!empty($params['jobarea'])) {    // 居住地
            $treated['area'] = explode(',', $params['jobarea']);
        }
        if (!empty($params['funtype'])) {    // 职能
            $treated['workfunc'] = explode(',', $params['funtype']);
        }
        if ($params['nearfuntype'] == '1') {    // 职能-只搜最近职能
            $treated['onlycurworkfunc'] = 1;
        }
        if (!empty($params['useexpsalary'])) {  // 期望薪资- 默认选择项
            $treated['expectsalary_min'] = self::$salaryRange[$params['useexpsalary']]['min'];
            $treated['expectsalary_max'] = self::$salaryRange[$params['useexpsalary']]['max'];
        } elseif (!empty($params['expsalary'])) {  // 期望薪资- 自定义选择项
            $range = explode(',', $params['expsalary']);
            $treated['expectsalary_min'] = $range[0] == '' ? 0 : $range[0];
            $treated['expectsalary_max'] = $range[1] == '' ? 0 : $range[1];
        }
        if (!empty($params['indtype'])) {    // 行业
            $treated['workindustry'] = explode(',', $params['indtype']);
        }
        if ($params['nearindtype'] == '1') {    // 行业-只搜最近行业
            $treated['onlycurindustry'] = 1;
        }

        $currYear = (int)date('Y');
        if (!empty($params['useworkyear'])) {    //工作年限-默认选择项
            $treated['workyear_min'] = $currYear - self::$workyearRange[$params['useworkyear']]['max'];
            $treated['workyear_max'] = $currYear - self::$workyearRange[$params['useworkyear']]['min'];
        } elseif (strlen($params['workyear']) > 0) {   //工作年限-自定义选择项
            $range = explode(',', $params['workyear']);
            $range[0] = $range[0] >= -1 ? $range[0] : -1;
            $range[1] = $range[1] >= -1 ? $range[1] : -1;
            if ($range[0] == -1 && $range[1] == -1) {    //不限 - 不限
                $treated['workyear_min'] = '0000';
                $treated['workyear_max'] = '0000';
            } elseif ($range[0] == -1 && $range[1] == 0) { // 不限 - 无工作经验
                $treated['workyear_min'] = '9999';
                $treated['workyear_max'] = '9999';
            } elseif ($range[0] == -1) { // 不限 - 具体工作年限
                $treated['workyear_min'] = $currYear - $range[1];
                $treated['workyear_max'] = '9999';
            } elseif ($range[0] == 0 && $range[1] == -1) { //无工作经验 - 及以上(不限)
                $treated['workyear_min'] = '0000';
                $treated['workyear_max'] = '9999';
            } elseif ($range[0] == 0 && $range[1] == 0) { //无工作经验 - 无工作经验
                $treated['workyear_min'] = '9999';
                $treated['workyear_max'] = '9999';
            } elseif ($range[0] == 0) { //无工作检验 - 具体工作年限
                $treated['workyear_min'] = $currYear - $range[1];
                $treated['workyear_max'] = '9999';
            } elseif ($range[1] == -1) {    //具体工作年限-不限
                $treated['workyear_min'] = '0000';
                $treated['workyear_max'] = $currYear - ($range[0] > 1 ? $range[0] : 0);
            } else {    //具体工作年限-具体工作年限
                $treated['workyear_min'] = $currYear - $range[1];
                $treated['workyear_max'] = $currYear - ($range[0] > 1 ? $range[0] : 0);
            }
        }
        if (isset($treated['workyear_min']) && $treated['workyear_min'] < 0) {
            unset($treated['workyear_min']);
        }
        if ($params['oversea'] === '1') {    //工作年限-海外工作经验
            $treated['extendstring'][] = '海外工作经历';
        }
        if (!empty($params['userdegree']) > 0) {    //学历-默认选择项
            $treated['degree_min'] = self::$degreeRange[$params['userdegree']]['min'];
            $treated['degree_max'] = self::$degreeRange[$params['userdegree']]['max'];
        } elseif (!empty($params['degree']) > 0) {    // 学历-自定义选择
            $range = explode(',', $params['degree']);
            if ($range[0] == 8 || $range[1] == 8) {
                //MBA 需要特殊处理
                $treated['ismba'] = 1;
                $treated['degree_min'] = 7;
                $treated['degree_max'] = 7;
            } else {
                $treated['degree_min'] = ($range[0] > 8) ? ($range[0] - 1) : $range[0];
                $treated['degree_max'] = ($range[1] > 8) ? ($range[1] - 1) : $range[1];
            }
        }
        if ($params['degree985'] == '1' && $params['degree211'] != '1') {    //学历-只选择 985
            $treated['extendstring'][] = '985院校';
        }
        if ($params['degree211'] == '1') {    // 学历- 选择了211
            $treated['extendstring'][] = '211院校';
        }
        if ($params['degreeallday'] == '1') {    //学历-全日制
            $treated['extendstring'][] = '全日制';
        }
        if ($params['degreeoversea'] == '1') {    //学历- 海外留学
            $treated['extendstring'][] = '海外学习经历';
        }
        if (!empty($params['usecursalary']) > 0) {    //目前月薪 - 默认选择项
            $treated['currentsalary_min'] = self::$salaryRange[$params['usecursalary']]['min'];
            $treated['currentsalary_max'] = self::$salaryRange[$params['usecursalary']]['max'];
        } elseif (!empty($params['cursalary'])) {    //目前月薪 - 自定义选择
            $range = explode(',', $params['cursalary']);
            $treated['currentsalary_min'] = $range[0] == '' ? 0 : $range[0];
            $treated['currentsalary_max'] = $range[1] == '' ? 0 : $range[1];
        }
        if (!empty($params['age'])) {    // 年龄
            $range = explode(',', $params['age']);
            $treated['age_min'] = $range[0];
            $treated['age_max'] = $range[1];
        }
        if (!empty($params['exparea'])) {    // 期望工作地
            $treated['expectarea'] = explode(',', $params['exparea']);
        }
        if (!empty($params['lang'])) {    // 语言水平 + 语言
            $treated['extendstring'][] = $params['lang'];
        }
        if (!empty($params['english'])) {    // 英语等级
            $treated['enlevel'] = $params['english'];
        }
        if ($params['gender'] == '1' || $params['gender'] == '2') {    // 性别
            $treated['gender'] = $params['gender'];
        }
        if (!empty($params['major'])) {    // 专业
            $treated['topmajor'] = explode(',', $params['major']);
        }
        if (!empty($params['register'])) {    // 户口
            $treated['hukou'] = $params['register'];
        }
        if (!empty($params['resumetime'])) {    // 简历更新时间
            $treated['updatedate_min'] = empty(self::$resumeUpdatedateRange[$params['resumetime']]['min']) ?
                '19980101' : date('Ymd', strtotime(self::$resumeUpdatedateRange[$params['resumetime']]['min']));
            $treated['updatedate_max'] = empty(self::$resumeUpdatedateRange[$params['resumetime']]['max']) ?
                date('Ymd') : date('Ymd', strtotime(self::$resumeUpdatedateRange[$params['resumetime']]['max']));
        }
        if ($params['jobstatus'] == '0' || $params['jobstatus'] == '1') {    // 求职状态
            $treated['currentsituation'] = $params['jobstatus'];
        }
        $treated['sorttype'] = $params['mgrsort'] == 1 ? 1 : 0; //排序规则  0 智能排序  1 时间排序
        $treated['extendstring'] = implode(' ', $treated['extendstring']);      //各类附加字段集合

        return $treated;
    }

    /**
     * 整合数组参数为sendbuf字符串
     * @param $params
     * @return string
     */
    protected function mergeSendBuf($params) {
        $sendBuf = '';
        foreach (self::$searchField as $field => $config) {
            if (!isset($params[$field])) {
                $sendBuf .= $config['default'];
            } else {
                if ($config['maxcount'] > 1 && is_array($params[$field])) {
                    $sendBuf .= implode('', array_slice($params[$field], 0, $config['maxcount']));
                } else {
                    $tmpBuffer = (string)$params[$field];
                    if (isset($config['charset'])) {
                        $tmpBuffer = Str::convertCharset($tmpBuffer, $config['charset']);
                    }
                    $sendBuf .= $tmpBuffer;
                }
            }
            $sendBuf .= ($config['separator'] == 'chr16') ? chr(16) : $config['separator'];
        }
        return $sendBuf;
    }

    /**
     * 解析引擎返回串
     * @return array
     */
    protected function parseReceiveBuf() {
        $length = strlen($this->receiveBuf);
        $iterator = 0;
        $result = [];
        while ($iterator * 4 < $length) {
            $data = 0;
            for ($i = 0; $i < 4 && $i + $iterator * 4 < $length; $i++) {
                $data += ord($this->receiveBuf[$iterator * 4 + $i]) * pow(2, 8 * $i);
            }
            $iterator++;
            $result[] = $data;
        }
        return [
            'callresult' => 1,
            'allMgrCount' => $result[0],               // 总记录数（符合搜索条件的精英ID，但超出10w就停止进一步搜索）
            'searchTime' => $result[1],                // 搜索用时（单位=10ns, 比如用了1.5ms, 则返回32位整数150）
            'mgrCount' => $result[2],                  // 当前返回数据总数（returncount）
            'mgrIDArr' => array_slice($result, 3),      // 数据总数个mgrID
        ];
    }

    /**
     * Notes:职能升级过渡
     * @param $params
     * @return mixed
     */
    public function functypeTransKeywords($params){
        $functypeTransMap = include ROOT_PATH . '/Data/Dict/functypetrans.php';
        $needTarnsFunctypes = array_column($functypeTransMap, 'code');
        $functype = explode(',', $params['funtype']);
        if (!$functype) {
            return $params;
        }
        //1.只要存在旧职能则不进行任何转换
        if (array_diff($functype, $needTarnsFunctypes)) {
            return $params;
        }
        $params['funtype'] = '';
        //2.如果不存在关键字，则将新职能使用or拼接关键字
        if (!$params['fulltext']) {
            $fulltextArr = [];
            foreach ($functype as $item) {
                $fulltextArr [] = $functypeTransMap[$item]['keyword'] ?? '';
            }
            $params['fulltext'] = implode(' or ', $fulltextArr);
            //3.如果存在关键字，则以关键字 and 拼接新职能1，如果存在or ，则每个关键字拼接新职能1
        } else {
            $fulltextArr = explode(' or ', $params['fulltext']);
            $tarnsFulltextArr = [];
            foreach ($fulltextArr as $item) {
                $tarnsFulltextArr []=  $item . ' ' . $functypeTransMap[$functype[0]]['keyword'] ?? '';
            }
            $params['fulltext'] = implode(' or ', $tarnsFulltextArr);
        }
        return $params;
    }

}